﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

using ConsolidationTool.Common;
using ConsolidationTool.WebParts.ControlTemplates;
using System.Data;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using System.IO;

namespace ConsolidationTool.WebParts.Layouts.CReport.Forecast
{
    public partial class HR : FSReportBase
    {
        public string strContent = string.Empty, strHeader = string.Empty;
        string year, month;
        public override ControlTemplates.CommonEntitySelector TopSelector
        {
            get
            {
                return commonEntitySelector as ControlTemplates.CommonEntitySelector;
            }
        }

        public override void BeforLoad()
        {
            if (!IsPostBack)
            {
                Hid_ActualSource.Value = Request.QueryString["Actual"];
                Hid_Forecast.Value = Request.QueryString["Forecast"];
                if (!Automatic)
                {
                    year = Common.BusinessService.GetConfiguration(Constants.Flash_Input_Year);
                    month = Common.BusinessService.GetConfiguration(Constants.Flash_Input_Month);
                }
            }
        }

        //创建表头
        public string CreateTableHead()
        {
            string strHeader1 = "<tr><td>Account Code</td><td>Name</td><td>Unit</td><td>&nbsp;</td>";
            #region --第一列
            year = TopSelector.yearTextBox.Text == "" ? "0" : TopSelector.yearTextBox.Text;
            month = TopSelector.monthTextBox.Text == "" ? "0" : TopSelector.monthTextBox.Text;
            for (int i = 1; i <= Convert.ToInt32(month); i++)
            {
                strHeader1 += "<td>" + year + "." + i.ToString("D2") + "</td>";
            }
            switch (month)
            {
                case "10":
                    strHeader1 += "<td>" + year + "." + (Convert.ToInt32(month) + 1).ToString("D2") + "</td>";
                    strHeader1 += "<td>" + year + "." + (Convert.ToInt32(month) + 2).ToString("D2") + "</td>";
                    strHeader1 += "<td>" + (Convert.ToInt32(year) + 1) + ".01 </td>";
                    break;
                case "11":
                    strHeader1 += "<td>" + year + "." + (Convert.ToInt32(month) + 1).ToString("D2") + "</td>";
                    strHeader1 += "<td>" + (Convert.ToInt32(year) + 1) + ".01 </td>";
                    strHeader1 += "<td>" + (Convert.ToInt32(year) + 1) + ".02 </td>";
                    break;
                case "12":
                    strHeader1 += "<td>" + (Convert.ToInt32(year) + 1) + ".01 </td>";
                    strHeader1 += "<td>" + (Convert.ToInt32(year) + 1) + ".02 </td>";
                    strHeader1 += "<td>" + (Convert.ToInt32(year) + 1) + ".03 </td>";
                    break;
                default:
                    strHeader1 += "<td>" + year + "." + (Convert.ToInt32(month) + 1).ToString("D2") + "</td>";
                    strHeader1 += "<td>" + year + "." + (Convert.ToInt32(month) + 2).ToString("D2") + "</td>";
                    strHeader1 += "<td>" + year + "." + (Convert.ToInt32(month) + 3).ToString("D2") + "</td>";
                    break;
            }
            strHeader1 += "</tr>";
            #endregion
            #region --第二列
            strHeader1 += "<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>";
            for (int i = 1; i <= Convert.ToInt32(month); i++)
            {
                strHeader1 += "<td>Actual</td>";
            }
            for (int i = 0; i < 3; i++)
            {
                strHeader1 += "<td>Forecast</td>";
            }
            //for (int i = 1; i <= 9 - Convert.ToInt32(month); i++)
            //{
            //    strHeader1 += "<td></td>";
            //}
            strHeader1 += "</tr>";
            #endregion
            return strHeader1;
        }

        public override void GenerateReport_Click(object sender, EventArgs e)
        {
            string ActualSource = Hid_ActualSource.Value; //"P_L";//Actual数据源
            string TempUrl = Hid_Forecast.Value + ".js";  // "PL.js";// 模版js
            string ForecastSource = "Forecast_" + Hid_Forecast.Value; // "Forecast_PL";// Forecast数据源
            strHeader = CreateTableHead();

            int month = TopSelector.monthTextBox.Text == "" ? 0 : Convert.ToInt32(TopSelector.monthTextBox.Text);
            DataTable actualdt = ForecastData.GetActualDt(TopSelector.lbCoCd.Text, TopSelector.yearTextBox.Text, month, ActualSource);
            var list = ForecastData.GetName(Server.MapPath("DataColumn/" + TempUrl));//获取js数据字段
            for (int m = 0; m < list.Count; m++)
            {
                string code = list[m].code.Trim();
                string name = list[m].desc;
                if (!string.IsNullOrEmpty(code) && !string.IsNullOrEmpty(name))
                {
                    List<fssubentity> subForecast;
                    string strForecast = ForecastData.GetForecast(TopSelector.lbCoCd.Text, TopSelector.yearTextBox.Text, month.ToString(), ForecastSource, code, Hid_Forecast.Value, out subForecast);//Forecast数据

                    strContent += "<tr>";
                    strContent += "<td>" + code.Replace("CM_", string.Empty) + "</td>";
                    strContent += "<td>" + name + "</td>";
                    if (actualdt.Rows.Count > 0)
                    {
                        for (int i = 0; i < actualdt.Rows.Count; i++)
                        {
                            #region --
                            if (i == 0)
                            {
                                if (Hid_Forecast.Value == "HR")
                                {
                                    strContent += "<td>number</td>";
                                    strContent += "<td></td>";
                                }
                                else
                                {
                                    if (name.IndexOf("/") > 0 && Hid_Forecast.Value == "PL")
                                        strContent += "<td>%</td>";
                                    else
                                        strContent += "<td>" + actualdt.Rows[i]["Currency"] + "</td>";
                                    strContent += "<td></td>";
                                }
                            }
                            if (code == "CM_FA02040101")
                                strContent += "<td>" + actualdt.Rows[i]["YTDM1_"] + "</td>";
                            else
                                strContent += "<td>" + actualdt.Rows[i][code] + "</td>";
                            #endregion
                        }
                    }
                    else
                    {
                        strContent += "<td></td>";
                        strContent += "<td></td>";
                    }
                    //填充Forecast前
                    for (int i = 0; i < month - actualdt.Rows.Count; i++)
                    {
                        strContent += "<td>0</td>";
                    }
                    string startd = "<td></td><td></td><td></td>";
                    strContent += strForecast == "" ? startd : strForecast;
                    //填充Forecast后
                    //for (int j = 1; j <= 9 - month; j++)
                    //{
                    //    strContent += "<td></td>";
                    //}
                    strContent += "</tr>";
                }
                else
                {
                    strContent += "<tr>";
                    if (!string.IsNullOrEmpty(code))
                        strContent += "<td>" + code + "</td><td></td>";
                    else if (!string.IsNullOrEmpty(name))
                        strContent += "<td></td><td>" + name + "</td>";
                    else
                        strContent += "<td></td><td></td>";
                    for (int i = 0; i < month + 5; i++)
                    {
                        strContent += "<td></td>";
                    }
                    strContent += "</tr>";
                }
            }
        }
    }
}
